<template>
	<!-- content -->
	<view class="warp">
		<view class="content">
			<up-textarea v-model="post.title" :focus="true" placeholder="距离热门就差这一帖啦" count border="none" height="100" ></up-textarea>
			<view class="picture">
				<up-upload
					:fileList="post.images"
					@afterRead="afterRead"
					@delete="deletePic"
					name="1"
					:multiple="true"
					:maxCount="9"
				></up-upload>
			</view>
		</view>
		<view class="tips">
			占位
		</view>
		<view class="submit">
			<up-button text="发布" size="large" type="primary" @click="pubilsh"></up-button>
		</view>
	</view>
</template>

<script setup>
	import { ref, defineEmits, defineProps } from 'vue'
	
	const props = defineProps({
		postItem:{
			type: Object,
			default: {
				images: [],
				title: ''
			}
		},
		edit: {
			type: Boolean,
			default: false
		},
	})
	const post = ref({...props.postItem})
	// 删除图片
	const deletePic = async (event) => {
		// 编辑
		if(props.edit){
			const fileID = post.value.images[event.index].fileID
			if(!fileID) return
			const _id = post.value._id
			uni.showLoading({
				title: '删除中',
				mask: true
			})
			const res = await uniCloud.callFunction({
				name: 'delete-fileID',
				data: { fileID, _id }
			})
			console.log(res);
			if(!res.result.success){
				uni.$u.toast('删除失败')
			}
			uni.hideLoading()
		}
		post.value.images.splice(event.index, 1)
	}
	// 本地上传
	const afterRead = async (event) => {
		event.file.forEach(item => {
			if(props.edit){
				item.isNew = true
			}
			post.value.images.push(item)
		})
	}
	const emit = defineEmits(['refSubmit'])
	
	// 发布
	const pubilsh = () => {
		emit('refSubmit', post.value)
	}
	
	// 清空内容
	const clearPost = () => {
		post.value = {
			images: [],
			title: ''
		}
	}
	
	defineExpose({ clearPost })
</script>

<style scoped>
	.topic{background-color: #fff;padding: 0 40rpx;margin-bottom: 10rpx;}
	.content{padding: 10rpx 40rpx 0;background-color: #fff;font-size: 28rpx;margin-bottom: 10rpx;}
	.content .picture{padding: 40rpx 0;}
	.tips{padding: 20rpx 40rpx;background-color: #fff;font-size: 28rpx;}
	.submit{padding: 0 40rpx;margin-top: 50rpx;}
</style>